/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.im.service.impl;

import me.zhengjie.im.domain.IssuanceM;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.im.repository.IssuanceMRepository;
import me.zhengjie.im.service.IssuanceMService;
import me.zhengjie.im.service.dto.IssuanceMDto;
import me.zhengjie.im.service.dto.IssuanceMQueryCriteria;
import me.zhengjie.im.service.mapstruct.IssuanceMMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yun
* @date 2024-02-20
**/
@Service
@RequiredArgsConstructor
public class IssuanceMServiceImpl implements IssuanceMService {

    private final IssuanceMRepository issuanceMRepository;
    private final IssuanceMMapper issuanceMMapper;

    @Override
    public PageResult<IssuanceMDto> queryAll(IssuanceMQueryCriteria criteria, Pageable pageable){
        Page<IssuanceM> page = issuanceMRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(issuanceMMapper::toDto));
    }

    @Override
    public List<IssuanceMDto> queryAll(IssuanceMQueryCriteria criteria){
        return issuanceMMapper.toDto(issuanceMRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public IssuanceMDto findById(Integer imId) {
        IssuanceM issuanceM = issuanceMRepository.findById(imId).orElseGet(IssuanceM::new);
        ValidationUtil.isNull(issuanceM.getImId(),"IssuanceM","imId",imId);
        return issuanceMMapper.toDto(issuanceM);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(IssuanceM resources) {
        
        int count = Integer.parseInt(resources.getImToken());
        for (int i = 0; i < count; i++) {
            IssuanceM newResource = new IssuanceM();
            BeanUtils.copyProperties(resources, newResource);
            newResource.setImBatch("FX" + newResource.getImBatch());
            newResource.setImToken("LP" + newResource.getImBatch() + i);
            issuanceMRepository.save(newResource);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(IssuanceM resources) {
        IssuanceM issuanceM = issuanceMRepository.findById(resources.getImId()).orElseGet(IssuanceM::new);
        ValidationUtil.isNull( issuanceM.getImId(),"IssuanceM","id",resources.getImId());
        issuanceM.copy(resources);
        issuanceMRepository.save(issuanceM);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer imId : ids) {
            issuanceMRepository.deleteById(imId);
        }
    }

    @Override
    public void download(List<IssuanceMDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (IssuanceMDto issuanceM : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("发行批次", issuanceM.getImBatch());
            map.put("发行令牌", issuanceM.getImToken());
            map.put("发行简介", issuanceM.getImBrief());
            map.put("发行状态", issuanceM.getImCode());
            map.put("发行通知", issuanceM.getImNotice());
            map.put("发行人", issuanceM.getImUser());
            map.put("发行时间", issuanceM.getImDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
